//
//  LQWelcomeViewController.swift
//  Weibo
//
//  Created by ios on 16/8/10.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
//欢迎页面
class LQWelcomeViewController: UIViewController {
  //MARK: -懒加载控件
    //背景图片
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconImageView:UIImageView = {
    let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //切圆角
    imageView.layer.cornerRadius = 45
    imageView.layer.masksToBounds = true
    return imageView
    }()
    
    //欢迎信息
    private lazy var messageLabel:UILabel = {
    let label = UILabel()
    label.text = "欢迎进入主页面"
    label.textColor = UIColor.redColor()
    label.font = UIFont.systemFontOfSize(15)
    return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI(){
    view.addSubview(iconImageView)
    view.addSubview(messageLabel)
    
    iconImageView.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(view)
        make.top.equalTo(view).offset(200)
        make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        
    messageLabel.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(view)
        make.top.equalTo(iconImageView.snp_bottom).offset(10)
        }
        
    }
    
  //  页面已经显示出来,执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation(){
        //  更新约束
        //  设置透明度
        messageLabel.alpha = 0
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
             self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
            UIView.animateWithDuration(1, animations: { () -> Void in
                
                self.messageLabel.alpha = 1
                
                }, completion: { (_) -> Void in
                    
                    //发送通知
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
            })
        }
    }

    
}
